package com.good.child.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.good.child.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create on: 2024/3/4
 * by Mr.Zhu
 * Description: TODO
 */
public class ItemMeunAdapter extends RecyclerView.Adapter<ItemMeunAdapter.MyViewHolder> {

    private Context context;
    private List<String> dataList;
    private onItemClick onItemClick;

    public ItemMeunAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<String> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public void setOnItemClick(onItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @NonNull
    @Override
    public ItemMeunAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //创建ViewHolder，返回每一项的布局
        View inflater = LayoutInflater.from(context).inflate(R.layout.item_item_menu, parent, false);
        ItemMeunAdapter.MyViewHolder myViewHolder = new ItemMeunAdapter.MyViewHolder(inflater);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        if (dataList == null || dataList.isEmpty()) {
            return;
        }
        String title = dataList.get(position);

        holder.nameTv.setText(title);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClick != null) {
                    onItemClick.onClickView(title);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 1 : dataList.size();
    }

    //内部类，绑定控件
    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_tv)
        TextView nameTv;
        @BindView(R.id.item_card)
        CardView cardView;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface onItemClick {
        void onClickView(String name);
    }
}
